package auto.app.controller;


import auto.app.controller.from.TestingEnvironmentAddFrom;
import auto.app.controller.from.TestingEnvironmentDeleteFrom;
import auto.app.controller.from.TestingEnvironmentPage;
import auto.app.controller.from.TestingEnvironmentUpdateFrom;
import auto.app.controller.info.TestEnvironmentSelectList;
import auto.app.controller.info.TestingEnvironmentInfo;
import auto.app.controller.info.TestingEnvironmentPageInfo;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.TestingEnvironment;
import auto.app.service.TestingEnvironmentService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static auto.app.exetion.MainExetionMessage.TESTINGENVIRONMENT;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@RestController
@RequestMapping("/testingEnvironment")
public class TestingEnvironmentController {
    @Autowired
    private TestingEnvironmentService testingEnvironmentService;


    //测试环境新增
    @PostMapping(value = "/add",name = "测试环境新增")
    public Result<Boolean> addTestingEnvironment(@Valid @RequestBody TestingEnvironmentAddFrom testingEnvironmentAddFrom) throws MainExection {
        //新增测试环境
        Boolean aBoolean = testingEnvironmentService.addTestingEnvironment(testingEnvironmentAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }
    //测试环境列表
    @PostMapping(value = "/list",name = "测试环境列表")
    public Result<TestingEnvironmentPageInfo>listTestingEnvironment(@RequestBody TestingEnvironmentPage testingEnvironmentPage){
        TestingEnvironmentPageInfo testingEnvironmentPageInfo = testingEnvironmentService.listTestingEnvironment(testingEnvironmentPage);
        return Result.getInstance(TestingEnvironmentPageInfo.class).setData(testingEnvironmentPageInfo);
    }
    //测试环境修改
    @PostMapping(value = "/update",name = "测试环境修改")
    public Result<Boolean> updateTestingEnvironment(@Valid @RequestBody TestingEnvironmentUpdateFrom testingEnvironmentUpdateFrom) throws MainExection {
        //对接口进行修改
        Boolean aBoolean = testingEnvironmentService.updateTestingEnvironment(testingEnvironmentUpdateFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }
    //删除测试环境数据
    @PostMapping(value = "/delete",name = "删除测试环境数据")
    public Result<Boolean> deleteids(@Valid @RequestBody TestingEnvironmentDeleteFrom testingEnvironmentDeleteFrom) throws HibernateExetion {
        //对接口进行修改
        Boolean deleteids = testingEnvironmentService.deleteids(testingEnvironmentDeleteFrom);
        return Result.getInstance(Boolean.class).setData(deleteids);
    }
    //测试环境查看详情
    @GetMapping(value = "/detail/{id}",name = "测试环境查看详情")
    public Result<TestingEnvironmentInfo> selectTestingEnvironment(@PathVariable String id) throws MainExection {
        TestingEnvironment testingEnvironmentServiceById = testingEnvironmentService.getById(id);
        if (testingEnvironmentServiceById ==null){
            throw new MainExection(TESTINGENVIRONMENT.getCode(),TESTINGENVIRONMENT.getMsg());
        }
        TestingEnvironmentInfo testingEnvironmentInfo = CommonUtil.copyOne(testingEnvironmentServiceById, TestingEnvironmentInfo.class);
        return Result.getInstance(TestingEnvironmentInfo.class).setData(testingEnvironmentInfo);
    }
    //测试环境下拉列表
    @GetMapping(value = "/testEnvironmentSelectList",name = "测试环境下拉列表")
    public Result<List<TestEnvironmentSelectList>> TestEnvironmentSelectList(){
        List<TestingEnvironment> list = testingEnvironmentService.list();
        List<TestEnvironmentSelectList> testEnvironmentSelectLists = CommonUtil.copyList(list, TestEnvironmentSelectList.class);
        return Result.getInstanceList(TestEnvironmentSelectList.class).setData(testEnvironmentSelectLists);
    }

}

